package main

import (
	"fmt"
	"time"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	x "gitcode.com/shexk/xframe/app"
)

func main() {
	x.GetApp().Start()

	// 创建一个新的Fyne应用
	myApp := app.New()
	// 创建主窗口，设置标题
	myWindow := myApp.NewWindow("XFrame Server")
	// 创建多行文本框
	textArea := widget.NewMultiLineEntry()

	scroll := container.NewScroll(textArea)
	scroll.SetMinSize(fyne.NewSize(800, 400)) // 设置最小尺寸

	numEdit := widget.NewEntry()
	sizeEdit := widget.NewEntry()
	clientNumEdit := widget.NewEntry()

	countContainer := container.NewGridWithColumns(8,
		widget.NewLabel("Plugins Num"),
		clientNumEdit,
		widget.NewLabel("Package Num"),
		numEdit,
		widget.NewLabel("Package Size"),
		sizeEdit,
	)

	go func() {
		timers := time.NewTicker(5 * time.Second)
		for range timers.C {
			fyne.Do(func() {
				// clientNumEdit.Text = fmt.Sprintf("%d", core.GetCount().ClientCount)
				// numEdit.Text = fmt.Sprintf("%d", core.GetCount().PackageCount)
				// sizeEdit.Text = core.GetValue(core.GetCount().PackageSize)
				clientNumEdit.Refresh()
				numEdit.Refresh()
				sizeEdit.Refresh()
			})
		}
	}()

	// 创建主容器，垂直排列按钮和文本框
	mainContainer := container.NewVBox(
		countContainer,
		scroll,
	)

	num := 0
	count := 0
	go func() {
		for {
			num++
			count++
			buf := <-x.GetLog().LogBus
			s := fmt.Sprintf("[%d]", count)
			fyne.Do(func() {
				// 安全执行
				textArea.SetText(s + buf + textArea.Text)
				if num >= 100 {
					textArea.SetText("")
					num = 0
				}
			})

		}
	}()
	myWindow.SetFixedSize(true)

	// 设置窗口内容并显示
	myWindow.CenterOnScreen()
	myWindow.SetContent(mainContainer)
	myWindow.Resize(fyne.NewSize(800, 400)) // 设置初始窗口大小

	myWindow.SetOnClosed(func() {

	})
	myWindow.Show()

	myApp.Run()

}

//打包程序：linux fyne package -os linux -icon app.ico
//Mac ../../bin/fyne package -os darwin -icon app.ico
//windows fyne package -os windows -icon app.ico
